﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace YubicoProvider
{
    class MembershipProvider : System.Web.Security.MembershipProvider
    {
        #region Global Objects
        private const string DEFAULT_NAME = "YubicoMembershipProvider";
        private const string DEFAULT_DESCRIPTION = "Yubico Membership Provider";
        private const string DEFAULT_VERIFY_ID = null;
        private WebServiceManager _webServiceManager;
        #endregion Global Objects


        #region Public Properties
        /// <summary>
        /// The verification identified supplied by Yubico
        /// </summary>
        public string VerifyID { get; set; }
        #endregion Public Properties


        #region Membership Provider Implementation

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            // Verify a valid config collection exists
            if (null == config)
                throw new ArgumentException("config");

            // Set the default values for the base initialize
            name = GetValue("name", name, DEFAULT_NAME);
            config["description"] = GetValue("description", config["description"], DEFAULT_DESCRIPTION);
            base.Initialize(name, config);

            // Get the custom values from the config.
            VerifyID = GetValue("verifyID", config["verifyID"], DEFAULT_VERIFY_ID);

            // Create the global instance of the WebServiceManager
            _webServiceManager = new WebServiceManager(VerifyID);
        }

        public override bool ValidateUser(string username, string password)
        {
            bool returnValue = false;
            string message = string.Empty;

            returnValue = _webServiceManager.Verify(password, out message);
            if (!returnValue)
            {
                // log failure here...
            }

            return returnValue;
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        #region Not Implemented
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }
        
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }
        #endregion Not Implemented

        #endregion Membership Provider Implementation


        #region Helper Methods
        /// <summary>
        /// Test the value for existance and returns the default when no value exists.
        /// </summary>
        /// <param name="name">The name of the parameter</param>
        /// <param name="value">The value of the parameter</param>
        /// <param name="defaultValue">The value to return when no value exists</param>
        /// <returns></returns>
        /// <remarks>
        /// Throw an ArgumentNullException if no value exists and the default is null.
        /// This allows and exeption to be thrown for values that should be required.
        /// </remarks>
        private string GetValue(string name, string value, string defaultValue)
        {
            string returnValue = null;

            if (!string.IsNullOrEmpty(value))
                returnValue = value;
            else
                returnValue = defaultValue;

            if (null == returnValue)
                throw new ArgumentNullException(name);

            return returnValue;
        }
        #endregion Helper Methods
    }
}
